<?php 
include ('./_config.inc.php');

$logfile = "/srv/www.linuxcounter.net/machine-updates.log";
$minimum = $lico['licoupdate_minimum'];
$newest = $lico['licoupdate_version'];

if (isset($_POST['senddata']) && trim($_POST['senddata']) != '') {
  if (preg_match("`.*##.*##.*##.*##.*##.*##.*##.*`", trim($_POST['senddata']))) {
    $data = explode("##", trim($_POST['senddata']));
    
    $script = urldecode(trim($data[0]));
    $tmp = explode(" ", $script);
    $script_name = trim($tmp[0]);
    $script_middle = trim($tmp[1]);
    $script_version = trim($tmp[2]);
    
    // lico-update.sh version 0.3.9
    
		if ($script_name != "lico-update.sh" && $script_middle != "version") {
      echo "You are not using our \"lico-update.sh\" script! Please get the most uptodate version here: http://linuxcounter.net/script.html. Otherwise your data could not be processed! \n";
      
    } else if (  version_compare($script_version, $minimum) == -1  ) {
      echo "New version of lico-update.sh (".$newest.") available. Your data will not be processed with the old version.\n";
      echo "Please download the new version here:  http://linuxcounter.net/script.html.\n";
      echo "Simply replace the old script, but make sure to chmod +x it.\n";
      
    } else {
      if (  ( version_compare($script_version, $minimum) == 0 || version_compare($script_version, $minimum) == 1  )  &&  version_compare($script_version, $newest) == -1  ) {
        echo "New version of lico-update.sh (".$newest.") available. You version still works and the data will be processed, but it is stongly recommended to update your lico-update.sh to the newest version.\n";
      }
      $name = urldecode(trim($data[1]));
      $cnumber = intval(urldecode(trim($data[2])));
      $mnumber = intval(urldecode(trim($data[3])));
      $cpu = urldecode(trim($data[4]));
      $cpunum = intval(urldecode(trim($data[5])));
      $accounts = intval(urldecode(trim($data[6])));
      $disk = urldecode(trim($data[7]));
      $totalram = urldecode(trim($data[8]));
      $users = intval(urldecode(trim($data[9])));
      $freedisk = urldecode(trim($data[10]));
      $freeram = urldecode(trim($data[11]));
      $totalswap = urldecode(trim($data[12]));
      $freeswap = urldecode(trim($data[13]));
      $flags = urldecode(trim($data[14]));
      $arch = urldecode(trim($data[15]));
      $kernel = urldecode(trim($data[16]));
      $uptime = urldecode(trim($data[17]));
      $load = urldecode(trim($data[18]));
      $cpufreq = urldecode(trim($data[19]));
      $distrib = urldecode(trim($data[20]));
      $distribversion = urldecode(trim($data[21]));
      $online = urldecode(trim($data[22]));
      $update_key = urldecode(trim($data[23]));
      
      
      $myipaddress = $_SERVER['REMOTE_ADDR'];
      // http://api.hostip.info/country.php?ip=77.4.153.160
      $country = lico_GetHTML('http://api.hostip.info/country.php?ip='.$myipaddress);
      $country = strtoupper(trim($country));
      if ($country == "XX")
        $country = "";
      
      if (preg_match("`\.`", $disk)) {
        $tmp = explode(".", $disk);
        $disk = trim($tmp[0]);
      }
      
      if (preg_match("`\.`", $freedisk)) {
        $tmp = explode(".", $freedisk);
        $freedisk = trim($tmp[0]);
      }
      
      if (preg_match("`open\s*suse`i", $distrib))
        $distrib = "openSUSE";
      else if (preg_match("`suse`i", $distrib))
        $distrib = "SuSE Linux";
      else if (preg_match("`s\.u\.s\.e`i", $distrib))
        $distrib = "SuSE Linux";
      else if (preg_match("`arch`i", $distrib))
        $distrib = "Arch Linux";
      else if (preg_match("`mint`i", $distrib))
        $distrib = "Linux Mint";
      else if (preg_match("`debian`i", $distrib))
        $distrib = "Debian GNU/Linux";
      else if (preg_match("`slack`i", $distrib))
        $distrib = "Slackware Linux";
      else if (preg_match("`gentoo`i", $distrib))
        $distrib = "Gentoo Linux";
      else if (preg_match("`xubuntu`i", $distrib))
        $distrib = "Xubuntu";
      else if (preg_match("`lubuntu`i", $distrib))
        $distrib = "Lubuntu";
      else if (preg_match("`kubuntu`i", $distrib))
        $distrib = "Kubuntu";
      else if (preg_match("`edubuntu`i", $distrib))
        $distrib = "Edubuntu";
      else if (preg_match("`ubuntu studio`i", $distrib))
        $distrib = "Ubuntu Studio";
      else if (preg_match("`mythbuntu`i", $distrib))
        $distrib = "Mythbuntu";
      else if (preg_match("`ubuntu`i", $distrib))
        $distrib = "Ubuntu";
      else if (preg_match("`fedora`i", $distrib))
        $distrib = "Fedora";
      else if (preg_match("`pclinux`i", $distrib))
        $distrib = "PCLinuxOS";
      else if (preg_match("`cent`i", $distrib))
        $distrib = "CentOS";
      else if (preg_match("`mageia`i", $distrib))
        $distrib = "Mageia";
      else if (preg_match("`freebsd`i", $distrib))
        $distrib = "FreeBSD";
      else if (preg_match("`openbsd`i", $distrib))
        $distrib = "OpenBSD";
      else if (preg_match("`netbsd`i", $distrib))
        $distrib = "NetBSD";
      else if (preg_match("`red\s*hat`i", $distrib))
        $distrib = "Red Hat Linux";
      else if (preg_match("`mandrake`i", $distrib))
        $distrib = "Mandrake";
      else if (preg_match("`mandriva`i", $distrib))
        $distrib = "Mandriva";
      else if (preg_match("`android`i", $distrib))
        $distrib = "Android";
      else if (preg_match("`knoppix`i", $distrib))
        $distrib = "Knoppix";
      else if (preg_match("`OpenWrt`i", $distrib))
        $distrib = "OpenWrt";
      else if (preg_match("`Mepis`i", $distrib))
        $distrib = "Mepis";
      else if (preg_match("`IPCOP`i", $distrib))
        $distrib = "IPCop";
      else if (preg_match("`Conectiva`i", $distrib))
        $distrib = "Conectiva";
      else if (preg_match("`Scientific`i", $distrib))
        $distrib = "Scientific Linux";
      else if (preg_match("`Xandros`i", $distrib))
        $distrib = "Xandros";
      else if (preg_match("`SmoothWall`i", $distrib))
        $distrib = "SmoothWall";
      else if (preg_match("`Mageia`i", $distrib))
        $distrib = "Mageia";
      else if (preg_match("`Maemo`i", $distrib))
        $distrib = "Maemo";
      else if (preg_match("`Sabayon`i", $distrib))
        $distrib = "Sabayon";
      else if (preg_match("`LFS`i", $distrib))
        $distrib = "Linux From Scratch";
      else if (preg_match("`Puppy`i", $distrib))
        $distrib = "Puppy Linux";
      else if (preg_match("`ClearOS`i", $distrib))
        $distrib = "ClearOS";
      else if (preg_match("`dsl`i", $distrib))
        $distrib = "Damn Small Linux";
      else if (preg_match("`Damn Small Linux`i", $distrib))
        $distrib = "Damn Small Linux";
      else if (preg_match("`netbsd`i", $distrib))
        $distrib = "NetBSD";
        
      else if (preg_match("`other`i", $distrib))
        $distrib = "";
      else if (preg_match("`unknown`i", $distrib))
        $distrib = "";
      else if (preg_match("`n/a`i", $distrib))
        $distrib = "";
      
      
      $array = array();
      
      $array['accounts']  = $accounts;
      $array['arch']    = $arch;
      $array['kernel']    = $kernel;
      $array['f_key']     = $mnumber;
      $array['mailer']    = '';
      $array['method']    = $script;
      $array['hostname']  = $name;
      $array['os']    = 'Linux';
      $array['owner']   = $cnumber;
      $array['users']   = $users;
      $array['MemTotal']  = $totalram;
      $array['MemFree'] = $freeram;
      $array['SwapTotal'] = $totalswap;
      $array['SwapFree']  = $freeswap;
      $array['diskspace_complete'] = $disk;
      $array['diskspace_free'] = $freedisk;
      $array['num_cores'] = $cpunum;
      $array['model_name'] = $cpu;
      $array['flags'] = $flags;
      $array['uptime'] = lico_getUptimeSeconds($uptime);
      $array['load'] = $load;
      $array['distribution'] = $distrib;
      $array['distribversion'] = $distribversion;
      $array['update_key'] = $update_key;
      $array['online'] = $online;
      $array['country'] = $country;
      
      $herz = $cpufreq;
      $herz = preg_replace("`\s*\(.*$`", "", $herz);
      $herz = str_replace("+", "", $herz);
      $herz = str_replace("XP", "", $herz);
      $herz = str_replace("xp", "", $herz);
      if (preg_match("`^X[0-9]+`", trim($herz))) {
        $array['model_name'] .= " ".trim($herz);
        $herz = "";
      }
      $herz = str_replace("X", "", $herz);
      $herz = str_replace("x", "", $herz);
      if (preg_match("`@`", trim($herz))) {
        $tmp = explode("@", $herz);
        $array['model_name'] .= " ".trim($tmp[0]);
        $herz = trim($tmp[1]);
      }
      if (preg_match("`^P`", trim($herz)) || preg_match("`^T`", trim($herz))) {
        $array['model_name'] .= " ".trim($herz);
        $herz = "";
      }
      
      if (!isset($array['online']) || trim($array['online']) == "") {
        $array['online'] = "offline";
      }
      
      $herz = str_replace("P", "", $herz);
      $herz = str_replace("p", "", $herz);
      $herz = str_replace("?", "", $herz);
      if (preg_match("`mhz`i", $herz)) {
        $herz = str_replace("mhz", "", trim($herz));
        $herz = str_replace("MHz", "", trim($herz));
        $herz = str_replace("MHZ", "", trim($herz));
        $herz = str_replace("Mhz", "", trim($herz));
        $herz = str_replace(".", "", $herz);
        $herz = str_replace(",", "", $herz);
        $herz = str_replace(" ", "", trim($herz));
      } else if (preg_match("`ghz`i", $herz)) {
        $herz = str_replace("ghz", "", trim($herz));
        $herz = str_replace("GHz", "", trim($herz));
        $herz = str_replace("GHZ", "", trim($herz));
        $herz = str_replace("Ghz", "", trim($herz));
        $herz = str_replace(",", ".", $herz);
        $herz = str_replace(" ", "", trim($herz));
        $herz = (float)$herz;
        $herz = intval(($herz * 1000));
      } else if (preg_match("`gh`i", $herz)) {
        $herz = str_replace("gh", "", trim($herz));
        $herz = str_replace("GH", "", trim($herz));
        $herz = str_replace("GH", "", trim($herz));
        $herz = str_replace("Gh", "", trim($herz));
        $herz = str_replace(",", ".", $herz);
        $herz = str_replace(" ", "", trim($herz));
        $herz = (float)$herz;
        $herz = intval(($herz * 1000));
      } else if (preg_match("`hz`i", $herz)) {
        $herz = str_replace("hz", "", trim($herz));
        $herz = str_replace("Hz", "", trim($herz));
        $herz = str_replace("HZ", "", trim($herz));
        $herz = str_replace(".", "", $herz);
        $herz = str_replace(",", "", $herz);
        $herz = str_replace(" ", "", trim($herz));
        $herz = round(intval($herz)/1000);
      }
      $array['herz'] = intval(round($herz));
      
      if (isset($array['owner']) && intval($array['owner']) >= 1) {
        if (isset($array['f_key']) && intval($array['f_key']) >= 1) {
          if (isset($array['update_key']) && trim($array['update_key']) != "") {
          
            $selm = "SELECT `cpu` FROM `machines` WHERE `f_key` = '".lico_ESC($array['f_key'])."' AND `owner` = '".lico_ESC($array['owner'])."' LIMIT 0,1;";
            $retm = $db->aselect($selm);
            if (isset($retm[0]) && intval($retm[0]['cpu']) >= 1) {
              $oldcpuid = intval($retm[0]['cpu']);
            }
            
            $cpu = trim($array['model_name']);
            $cpu = preg_replace("`\s+`", " ", $cpu);
            $herz = intval($array['herz']);
            if (preg_match("`@`", $cpu)) {
              $tmp = explode("@", $cpu);
              $cpu = trim($tmp[0]);
              $herz = trim($tmp[1]);
              $cpu = preg_replace("`\s+`", " ", $cpu);
              $herz = preg_replace("`\s*\(.*$`", "", $herz);
              $herz = str_replace("+", "", $herz);
              $herz = str_replace("XP", "", $herz);
              $herz = str_replace("xp", "", $herz);
              if (preg_match("`^X[0-9]+`", trim($herz))) {
                $array['model_name'] .= " ".trim($herz);
                $herz = "";
              }
              $herz = str_replace("X", "", $herz);
              $herz = str_replace("x", "", $herz);
              if (preg_match("`@`", trim($herz))) {
                $tmp = explode("@", $herz);
                $array['model_name'] .= " ".trim($tmp[0]);
                $herz = trim($tmp[1]);
              }
              if (preg_match("`^P`", trim($herz)) || preg_match("`^T`", trim($herz))) {
                $array['model_name'] .= " ".trim($herz);
                $herz = "";
              }
          
              $herz = str_replace("P", "", $herz);
              $herz = str_replace("p", "", $herz);
              $herz = str_replace("?", "", $herz);
              if (preg_match("`mhz`i", $herz)) {
                $herz = str_replace("mhz", "", trim($herz));
                $herz = str_replace("MHz", "", trim($herz));
                $herz = str_replace("MHZ", "", trim($herz));
                $herz = str_replace("Mhz", "", trim($herz));
                $herz = str_replace(".", "", $herz);
                $herz = str_replace(",", "", $herz);
                $herz = str_replace(" ", "", trim($herz));
              } else if (preg_match("`ghz`i", $herz)) {
                $herz = str_replace("ghz", "", trim($herz));
                $herz = str_replace("GHz", "", trim($herz));
                $herz = str_replace("GHZ", "", trim($herz));
                $herz = str_replace("Ghz", "", trim($herz));
                $herz = str_replace(",", ".", $herz);
                $herz = str_replace(" ", "", trim($herz));
                $herz = (float)$herz;
                $herz = intval(($herz * 1000));
              } else if (preg_match("`gh`i", $herz)) {
                $herz = str_replace("gh", "", trim($herz));
                $herz = str_replace("GH", "", trim($herz));
                $herz = str_replace("GH", "", trim($herz));
                $herz = str_replace("Gh", "", trim($herz));
                $herz = str_replace(",", ".", $herz);
                $herz = str_replace(" ", "", trim($herz));
                $herz = (float)$herz;
                $herz = intval(($herz * 1000));
              } else if (preg_match("`hz`i", $herz)) {
                $herz = str_replace("hz", "", trim($herz));
                $herz = str_replace("Hz", "", trim($herz));
                $herz = str_replace("HZ", "", trim($herz));
                $herz = str_replace(".", "", $herz);
                $herz = str_replace(",", "", $herz);
                $herz = str_replace(" ", "", trim($herz));
                $herz = round(intval($herz)/1000);
              }
            }
            
            
            
            
            // $update_key
            $selectup = "SELECT `f_key`, `country` FROM `machines` WHERE `f_key`='".lico_ESC($array['f_key'])."' AND `owner`='".lico_ESC($array['owner'])."' AND `update_key`='".lico_ESC($array['update_key'])."' LIMIT 0,1;";
            $seluserp = $db->aselect($selectup);
            if (isset($seluserp[0]['f_key']) && intval($seluserp[0]['f_key']) >= 1) {
              $oldcountry = strtoupper(stripslashes(trim($seluserp[0]['country'])));
              if ($oldcountry != "") {
                $selplace = "SELECT * FROM `places` WHERE `code`='".lico_ESC($oldcountry)."' LIMIT 0,1;";
                $retplace = $db->aselect($selplace);
                if (isset($retplace[0]['f_key']) && intval($retplace[0]['f_key']) >= 1) {
                  $updplace = "UPDATE `places` SET `hostcount`=`hostcount`-1 WHERE `code`='".lico_ESC($oldcountry)."'";
                  $db->query($updplace);
                } else {
                  $oldcountry = "";
                }
              }
              $cpu = preg_replace("`\s+`", " ", $cpu);
              $select = "SELECT * FROM `processors` WHERE `name`='".lico_ESC($cpu)."' AND `herz`='".lico_ESC($herz)."' LIMIT 0,1;";
              $seluser = $db->aselect($select);
              if (isset($seluser[0]) && intval($seluser[0]['f_key']) >= 1) {
                $cpuid = intval($seluser[0]['f_key']);
                if ($cpuid != $oldcpuid) {
                  $upd = "UPDATE `processors` SET `machines`=`machines`+1 WHERE `f_key`='".$cpuid."';";
                  $db->query($upd);
                }
              } else {
                $upd = "UPDATE `processors` SET `machines`=`machines`-1 WHERE `f_key`='".$oldcpuid."';";
                $db->query($upd);
                $upd = "UPDATE `processors` SET `machines`=0 WHERE `machines`<0;";
                $db->query($upd);
                $ins = "INSERT INTO `processors` SET `name`='".lico_ESC($cpu)."', `herz`='".lico_ESC($herz)."', `machines`=1;";
                $db->query($ins);
                $select = "SELECT * FROM `processors` WHERE `name`='".lico_ESC($cpu)."' AND `herz`='".lico_ESC($herz)."' LIMIT 0,1;";
                $seluser = $db->aselect($select);
                $cpuid = intval($seluser[0]['f_key']);
              }
              
              $upd = "UPDATE `machines` SET ";
              $upd .= "                     `f_mtime` = '".date("Y-m-d H:i:s", time())."', ";
              $upd .= "                     `name` = '".lico_ESC($array['hostname'])."', ";
              $upd .= "                     `cpu` = '".lico_ESC($cpuid)."', ";
              $upd .= "                     `cpunum` = '".lico_ESC($array['num_cores'])."', ";
              $upd .= "                     `accounts` = '".lico_ESC($array['accounts'])."', ";
              if ($array['country'] != "") {
                unset($retplace);
                $selplace = "SELECT * FROM `places` WHERE `code`='".lico_ESC($array['country'])."' LIMIT 0,1;";
                $retplace = $db->aselect($selplace);
                if (isset($retplace[0]['f_key']) && intval($retplace[0]['f_key']) >= 1) {
                  $updplace = "UPDATE `places` SET `hostcount`=`hostcount`+1 WHERE `code`='".lico_ESC($array['country'])."'";
                  $db->query($updplace);
                } else {
                  $array['country'] = "";
                }
                $upd .= "                     `country` = '".lico_ESC($array['country'])."', ";
              }
              $upd .= "                     `disk` = '".lico_ESC(round((intval($array['diskspace_complete']) / 1024)))."', ";
              if ($array['mailer'] != "")
                $upd .= "                     `mailer` = '".lico_ESC(ucfirst($array['mailer']))."', ";
              $upd .= "                     `memory` = '".lico_ESC(round((intval($array['MemTotal']) / 1024)))."', ";
              $upd .= "                     `users` = '".lico_ESC($array['users'])."', ";
              $upd .= "                     `diskfree` = '".lico_ESC(round((intval($array['diskspace_free']) / 1024)))."', ";
              $upd .= "                     `memfree` = '".lico_ESC(round((intval($array['MemFree']) / 1024)))."', ";
              $upd .= "                     `swap` = '".lico_ESC(round((intval($array['SwapTotal']) / 1024)))."', ";
              $upd .= "                     `swapfree` = '".lico_ESC(round((intval($array['SwapFree']) / 1024)))."', ";
              $upd .= "                     `cpuflags` = '".lico_ESC($array['flags'])."', ";
              $upd .= "                     `arch` = '".lico_ESC($array['arch'])."', ";
              $upd .= "                     `kernel` = '".lico_ESC($array['kernel'])."', ";
              $upd .= "                     `online` = '".lico_ESC($array['online'])."', ";
              $upd .= "                     `uptime` = '".lico_ESC($array['uptime'])."', ";
              $upd .= "                     `distribution` = '".lico_ESC($array['distribution'])."', ";
              $upd .= "                     `distversion` = '".lico_ESC($array['distribversion'])."', ";
              $upd .= "                     `load` = '".lico_ESC($array['load'])."', ";
              $upd .= "                     `isactive` = 'YES' ";
              $upd .= "             WHERE   `f_key` = '".lico_ESC($array['f_key'])."' AND `owner` = '".lico_ESC($array['owner'])."' AND `update_key`='".lico_ESC($array['update_key'])."';";
              $db->query($upd);
              
              $fp = fopen($logfile, "a");
              fwrite($fp, "[".date("Y-m-d H:i:s", $aktts)."] owner=".$array['owner']."\tmachine=".$array['f_key']."\tupdate_key=".$array['update_key']."\tcpuid=".$cpuid."\tcpu=".$cpu."\therz=".$herz."\thostname=".$array['hostname']."\tversion=".$script_version."\n");
              fclose($fp);
              @chmod($logfile, 0777);
            } else {
              echo "The machine you want to update is not yours or your update_key is wrong.\n";
              echo "Maybe you have forgotten to save your machine in your profile once to store the update_key?";
            }
            
          } else {
            echo "You have not set the update_key for your machine! Please get the newest version of this script (\"".$lico['licoupdate_version']."\")\n";
            echo "and get the update_key for the machine you want to update from your profile. You can see the update_key for\n";
            echo "the machine when editing the machine and then looking into the field \"LiCo Update Key\". You also\n";
            echo "are able to change the key there, if you want.\n";
            echo "ATTENTION: You would have to save the machine in your profile once to store the update_key!\n";
            echo "Put both into \"lico-update.sh -i\".\n";
          }
          
        } else {
          $cpu = trim($array['model_name']);
          $cpu = preg_replace("`\s+`", " ", $cpu);
          $herz = intval($array['herz']);
          if (preg_match("`@`", $cpu)) {
            $tmp = explode("@", $cpu);
            $cpu = trim($tmp[0]);
            $herz = trim($tmp[1]);
            $cpu = preg_replace("`\s+`", " ", $cpu);
            $herz = preg_replace("`\s*\(.*$`", "", $herz);
            $herz = str_replace("+", "", $herz);
            $herz = str_replace("XP", "", $herz);
            $herz = str_replace("xp", "", $herz);
            if (preg_match("`^X[0-9]+`", trim($herz))) {
              $array['model_name'] .= " ".trim($herz);
              $herz = "";
            }
            $herz = str_replace("X", "", $herz);
            $herz = str_replace("x", "", $herz);
            if (preg_match("`@`", trim($herz))) {
              $tmp = explode("@", $herz);
              $array['model_name'] .= " ".trim($tmp[0]);
              $herz = trim($tmp[1]);
            }
            if (preg_match("`^P`", trim($herz)) || preg_match("`^T`", trim($herz))) {
              $array['model_name'] .= " ".trim($herz);
              $herz = "";
            }
        
            $herz = str_replace("P", "", $herz);
            $herz = str_replace("p", "", $herz);
            $herz = str_replace("?", "", $herz);
            if (preg_match("`mhz`i", $herz)) {
              $herz = str_replace("mhz", "", trim($herz));
              $herz = str_replace("MHz", "", trim($herz));
              $herz = str_replace("MHZ", "", trim($herz));
              $herz = str_replace("Mhz", "", trim($herz));
              $herz = str_replace(".", "", $herz);
              $herz = str_replace(",", "", $herz);
              $herz = str_replace(" ", "", trim($herz));
            } else if (preg_match("`ghz`i", $herz)) {
              $herz = str_replace("ghz", "", trim($herz));
              $herz = str_replace("GHz", "", trim($herz));
              $herz = str_replace("GHZ", "", trim($herz));
              $herz = str_replace("Ghz", "", trim($herz));
              $herz = str_replace(",", ".", $herz);
              $herz = str_replace(" ", "", trim($herz));
              $herz = (float)$herz;
              $herz = intval(($herz * 1000));
            } else if (preg_match("`gh`i", $herz)) {
              $herz = str_replace("gh", "", trim($herz));
              $herz = str_replace("GH", "", trim($herz));
              $herz = str_replace("GH", "", trim($herz));
              $herz = str_replace("Gh", "", trim($herz));
              $herz = str_replace(",", ".", $herz);
              $herz = str_replace(" ", "", trim($herz));
              $herz = (float)$herz;
              $herz = intval(($herz * 1000));
            } else if (preg_match("`hz`i", $herz)) {
              $herz = str_replace("hz", "", trim($herz));
              $herz = str_replace("Hz", "", trim($herz));
              $herz = str_replace("HZ", "", trim($herz));
              $herz = str_replace(".", "", $herz);
              $herz = str_replace(",", "", $herz);
              $herz = str_replace(" ", "", trim($herz));
              $herz = round(intval($herz)/1000);
            }
          }
          
          
          
          $selecte = "SELECT `f_key` FROM `machines` WHERE `name`='".lico_ESC($array['hostname'])."' AND `owner`='".lico_ESC($array['owner'])."' LIMIT 0,1;";
          $selmachine = $db->aselect($selecte);
          if (isset($selmachine[0]['f_key']) && intval($selmachine[0]['f_key']) >= 1) {
            echo "A machine with the same hostname already exists in your account. Did you want to update this one?\n";
            echo "If so, please login to your account, go to your profile and get your machine key and your update_key.You can\n";
            echo "see the update_key for the machine when editing the machine and then looking into the field \n";
            echo "\"LiCo Update Key\". You also are able to change the key there, if you want.\n";
            echo "ATTENTION: You would have to save the machine in your profile once to store the update_key!\n";
            echo "Put both into \"lico-update.sh -i\".\n";
          } else {
            $cpu = preg_replace("`\s+`", " ", $cpu);
            $select = "SELECT * FROM `processors` WHERE `name`='".lico_ESC($cpu)."' AND `herz`='".lico_ESC($herz)."' LIMIT 0,1;";
            $seluser = $db->aselect($select);
            if (isset($seluser[0]) && intval($seluser[0]['f_key']) >= 1) {
              $cpuid = intval($seluser[0]['f_key']);
            } else {
              $ins = "INSERT INTO `processors` SET `name`='".lico_ESC($cpu)."', `herz`='".lico_ESC($herz)."', `machines`=1;";
              $db->query($ins);
              $select = "SELECT * FROM `processors` WHERE `name`='".lico_ESC($cpu)."' AND `herz`='".lico_ESC($herz)."' LIMIT 0,1;";
              $seluser = $db->aselect($select);
              $cpuid = intval($seluser[0]['f_key']);
            }
            
            $ctime = date("Y-m-d H:i:s", time());
            
            $update_key = lico_GenRandomString(24, PWD_ALLOW_ALL);
            $upd = "INSERT INTO `machines` SET ";
            $upd .= "                     `owner` = '".lico_ESC($array['owner'])."', ";
            $upd .= "                     `f_ctime` = '".$ctime."', ";
            $upd .= "                     `f_mtime` = '".$ctime."', ";
            $upd .= "                     `name` = '".lico_ESC($array['hostname'])."', ";
            $upd .= "                     `cpu` = '".lico_ESC($cpuid)."', ";
            $upd .= "                     `cpunum` = '".lico_ESC($array['num_cores'])."', ";
            $upd .= "                     `accounts` = '".lico_ESC($array['accounts'])."', ";
            $upd .= "                     `country` = '".lico_ESC($array['country'])."', ";
            $upd .= "                     `disk` = '".lico_ESC(round((intval($array['diskspace_complete']) / 1024)))."', ";
            $upd .= "                     `mailer` = '".lico_ESC(ucfirst($array['mailer']))."', ";
            $upd .= "                     `memory` = '".lico_ESC(round((intval($array['MemTotal']) / 1024)))."', ";
            $upd .= "                     `users` = '".lico_ESC($array['users'])."', ";
            $upd .= "                     `diskfree` = '".lico_ESC(round((intval($array['diskspace_free']) / 1024)))."', ";
            $upd .= "                     `memfree` = '".lico_ESC(round((intval($array['MemFree']) / 1024)))."', ";
            $upd .= "                     `swap` = '".lico_ESC(round((intval($array['SwapTotal']) / 1024)))."', ";
            $upd .= "                     `swapfree` = '".lico_ESC(round((intval($array['SwapFree']) / 1024)))."', ";
            $upd .= "                     `cpuflags` = '".lico_ESC($array['flags'])."', ";
            $upd .= "                     `arch` = '".lico_ESC($array['arch'])."', ";
            $upd .= "                     `kernel` = '".lico_ESC($array['kernel'])."', ";
            $upd .= "                     `online` = '".lico_ESC($array['online'])."', ";
            $upd .= "                     `distribution` = '".lico_ESC($array['distribution'])."', ";
            $upd .= "                     `distversion` = '".lico_ESC($array['distribversion'])."', ";
            $upd .= "                     `uptime` = '".lico_ESC($array['uptime'])."', ";
            $upd .= "                     `load` = '".lico_ESC($array['load'])."', ";
            $upd .= "                     `isactive` = 'YES', ";
            $upd .= "                     `update_key` = '".lico_ESC($update_key)."'; ";
            $db->query($upd);
            
            $selectnm = "SELECT f_key FROM `machines` WHERE `update_key`='".lico_ESC($update_key)."' AND `owner` = '".lico_ESC($array['owner'])."' LIMIT 0,1;";
            $retnm = $db->aselect($selectnm);
            if (isset($retnm[0]) && intval($retnm[0]['f_key']) >= 1) {
              $newmachine = intval($retnm[0]['f_key']);
            }
            
            $fp = fopen($logfile, "a");
            fwrite($fp, "[".$ctime."] owner=".$array['owner']."\tmachine=NEW\tupdate_key=".$array['update_key']."\tcpuid=".$cpuid."\tcpu=".$cpu."\therz=".$herz."\thostname=".$array['hostname']."\tversion=".$script_version."\n");
            fclose($fp);
            chmod($logfile, 0777);
            
            echo "Since you have not submitted a machine key, this has now created a new machine within your account.\n";
            echo "The machine number and the update_key follows. Put both into \"lico-update.sh -i\": \n";
            echo "The new machine number is: ".$newmachine." \n";
            echo "The machine update_key is: ".$update_key." \n";
          }
        }
        
        
      } else {}
    }
  }
}






if ((isset($_GET['login']) && intval($_GET['login']) == 1) && (isset($_GET['passwd']) && trim($_GET['passwd']) != '') && (isset($_GET['userid']) && trim($_GET['userid']) != '')) {
  $password = md5(trim($_GET['passwd']));
  $userid = trim($_GET['userid']);
  if ($userid == "") {
    echo "false";
    exit;
  } else {
    if (is_numeric($userid)) {
      $where = "f_key";
    } else {
      $where = "username";
    }
    $select = "SELECT * FROM `users` WHERE `".$where."`='".lico_ESC($userid)."' AND `login`='".lico_ESC($password)."' AND `".$where."`!=''";
    $seluser = $db->unbuffered_query_first($select);
    if (isset($seluser[0]) && intval($seluser[0]) >= 1) {
      $email = trim(stripslashes($seluser[5]));
      echo "ok\n";
      echo $email;
      exit;
    } else {
      echo "false";
      exit;
    }
  }
}














?>